import { query, remove, add, edit, queryGet, queryOne } from '../services/extendForm';

/**
 *
 * 查询请求统一使用Get
 * 新增/编辑请求统一使用 Post/Put
 * 删除请求统一使用 Delete
 *
 */
export default {
  namespace: 'extendForm',
  state: {
    data: {
      list: [],
      pagination: {},
    },
    listData: [],
    loading: true,
    resultData: {}, // 其他查询请求
  },

  effects: {


    /**############################   table    ###############################################*/


    /** 分页条件查询  GET请求*/
    *fetch({ payload, callback }, { call, put }) {
      const response = yield call(query, payload);
      yield put({
        type: 'save',
        payload: { data: response, loading: false },
      });
      if (callback) callback(response);
    },
    /** 普通条件查询  GET请求*/
    *list({ payload, callback }, { call, put }) {
      const response = yield call(query, payload);
      yield put({
        type: 'save',
        payload: { listData: response, loading: false },
      });
      if (callback) callback(response);
    },
    /** 提交对象数据  POST请求*/
    *add({ payload, callback }, { call, put }) {
      const response = yield call(add, payload);
      yield put({
        type: 'save',
        payload: { data: response, loading: false },
      });
      if (callback) callback(response);
    },
    /** 编辑对象数据  PUT请求  格式等同于新增，必传参数sid或者id  */
    *edit({ payload, callback }, { call, put }) {
      const response = yield call(edit, payload);
      yield put({
        type: 'save',
        payload: { data: response, loading: false },
      });
      if (callback) callback(response);
    },
    /** 删除  DELETE请求，必传参数sid或者id  */
    *remove({ payload, callback }, { call }) {
      const response = yield call(remove, payload);
      yield put({
        type: 'save',
        payload: { data: response, loading: false },
      });
      if (callback) callback(response);
    },

    /**########################    other      ##############################################################*/


    /** 查询    GET请求       参数使用 ？的形式，与fetch等同，response 存在resultData， 不会刷新table数据 */
    *queryGet({ payload, callback }, { call, put }) {
      const response = yield call(queryGet, payload);
      yield put({
        type: 'save',
        payload: { resultData: response, loading: false },
      });
      if (callback) callback(response);
    },
    /** 查询  GET请求       参数使用 / 的形式  查询单个数据使用  参数 sid/id   */
    *queryOne({ payload, callback }, { call, put }) {
      const response = yield call(queryOne, payload);
      yield put({
        type: 'save',
        payload: { resultData: response, loading: false },
      });
      if (callback) callback(response);
    },
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};
